/**
 * Created by gongxun on 16/11/19.
 */

var KwxCardLayer = cc.Layer.extend({
    //存储手牌
    mBatchNode : null,
    //玩家model
    mPlayers : null,
    //最后出牌玩家
    mLastDealCardSeat : null,
    //最后一个牌标记
    mLastPointerSprite : null,
    //滑动出牌的牌
    mSelectMoveSprite : null,
    //胡牌特效
    mWinDian1 : null,
    mWinDian2 : null,
    //创建牌的编号
    mCreatCardIndex : 0,
    ctor : function(){
        this._super();

        //sprite
        cc.spriteFrameCache.addSpriteFrames("res/bigCards.plist", "res/bigCards.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");
        cc.spriteFrameCache.addSpriteFrames("res/effect/cardFire.plist", "res/effect/cardFire.png");
        cc.spriteFrameCache.addSpriteFrames("res/effect/cardLightning.plist", "res/effect/cardLightning.png");

        //虎牌点
        this.mBatchNode = new cc.Node();
        this.addChild(this.mBatchNode);

        //最后一个牌标记
        this.mLastPointerSprite = new cc.Sprite("res/ccs/table/starpointer.png");
        this.mLastPointerSprite.setVisible(false);
        this.mLastPointerSprite.setScale(1);
        this.mLastPointerSprite.setOpacity(235);
        this.mLastPointerSprite.setAnchorPoint(cc.p(0.5, 0));
        this.addChild(this.mLastPointerSprite);

        //胡牌的粒子
        this.mWinDian1 = new cc.Sprite();
        this.mWinDian1.setVisible(false);
        this.addChild(this.mWinDian1);

        this.mWinDian2 = new cc.Sprite();
        this.mWinDian2.setVisible(false);
        this.addChild(this.mWinDian2);

        //player
        this.mPlayers = [];
        for (var i = 0; i < 3; i++){
            var palyer = null;
            if (i == 0){
                palyer = new KwxPlayerMyModel();
                palyer.loadTouchListener(this);
            }else{
                palyer = new KwxPlayerModel();
            }
            palyer.seat = i;
            this.mPlayers.push(palyer);
        }

        this.schedule(function(dt){
            var cards = this.mBatchNode.getChildren();
            for (var i = 0; i < cards.length; i++){
                cards[i].setLocalZOrder(cards[i].getCardZ());
            }
        }, this);
    },

    onEnter : function(){
        this._super();

    },

    removeAllCard : function(){
        this.mCreatCardIndex = 0;
        this.mBatchNode.removeAllChildren();
        for (var i = 0; i < 3; i++){
            this.mPlayers[i].reset();
        }

        this.mSelectMoveSprite = null;

        this.setStopWinAnim();
    },

    setLastPointerShow : function(isShow, point){
        if (isShow){
            this.mLastPointerSprite.setVisible(true);
            this.mLastPointerSprite.stopAllActions();
            this.mLastPointerSprite.setPosition(point);
            this.mLastPointerSprite.runAction(
                cc.repeatForever(
                    cc.sequence(
                        cc.moveBy(0.5, cc.p(0, 10)),
                        cc.moveBy(0.5, cc.p(0, -10))
                    )
                )
            );
        }else{
            this.mLastPointerSprite.setVisible(false);
            this.mLastPointerSprite.stopAllActions();
        }
    },

    getKwxCard : function(){
        var kwxCard = new KwxCard();
        kwxCard.mCreatIndex = this.mCreatCardIndex++;
        this.mBatchNode.addChild(kwxCard);
        return kwxCard;
    },

    //设置手牌和发牌动画
    setInhandCards : function(seat, cardNums){
        var cards = [];
        for (var i = 0; i < cardNums.length; i++){
            var card = this.getKwxCard();
            card.setCardNum(cardNums[i]);
            cards.push(card);
        }
        this.mPlayers[seat].setInhandCards(cards);
    },

    //添加手牌
    addInhandCard : function(seat, cardNum){
        var card = this.getKwxCard();
        card.setCardNum(cardNum);
        this.mPlayers[seat].addInhandCard(card);
    },

    //打出手牌
    setDealCard : function(seat, cardNum){
        this.mPlayers[seat].setDealCard(cardNum);
        this.mLastDealCardSeat = seat;

        //标记
        var index = this.mPlayers[seat].dealCards.length - 1;
        var lastCard = this.mPlayers[seat].dealCards[index];
        var p = mo.kwxCardHelper.getDealCardPos(seat, index);
        var size = cc.size(lastCard.getContentSize().width * lastCard.getScaleX(), lastCard.getContentSize().height * lastCard.getScaleY());
        p.y += size.height / 2;
        this.setLastPointerShow(true, p);
    },

    //开口牌
    setOpenCard : function(seat, cardNums, openType){
        this.setLastPointerShow(false);

        var cards = [];

        //从牌桌拿走牌
        if (openType == 2 || openType == 4){
            //碰和明杠
            //找到牌桌上的牌
            var dealCard = this.mPlayers[this.mLastDealCardSeat].dealCards.pop();
            cards.push(dealCard);

            //剩下牌的号码
            for (var i = 0; i < cardNums.length; i++){
                if (cardNums[i] == dealCard.mCardNum){
                    cardNums.splice(i, 1);
                    break;
                }
            }
        }

        //从手牌中拿走牌
        for (var i = 0; i < cardNums.length; i++){
            var index = mo.kwxCardHelper.findCardForNum(this.mPlayers[seat].inhandCards, cardNums[i]);
            if (index != null){
                cards.push(this.mPlayers[seat].inhandCards[index]);
                this.mPlayers[seat].inhandCards.splice(index, 1);
            }else{
                //手牌中没有找到
                index = this.mPlayers[seat].inhandCards.length - 1;
                var card = this.mPlayers[seat].inhandCards[index];
                card.setCardNum(cardNums[i]);
                cards.push(card);
                this.mPlayers[seat].inhandCards.splice(index, 1);
            }
        }

        if (openType == 6){
            //补杠找到碰
            var openCards = this.mPlayers[seat].openCards;
            for (var i = 0; i < openCards.length; i++){
                var openCard = openCards[i];
                if (openCard.type == 2 && openCard.cards[0].mCardNum == cardNums[0]){
                    openCard.type = 6;
                    openCard.cards.push(cards[0]);
                    break;
                }
            }
        }else{
            var openCard = mo.kwxCardHelper.getOpenCardStruct(cards, openType);
            this.mPlayers[seat].openCards.push(openCard);
        }

        this.mPlayers[seat].setOpenCard();
    },

    //玩家手牌中的亮的牌
    setLiangCard : function(seat, liangCardNums){
        if (seat != 0){
            this.mPlayers[seat].setLiangCard(liangCardNums);
        }
    },

    //玩家手中扣的牌
    setKouCard : function(seat, kouCardNums){
        this.mPlayers[seat].setKouCard(kouCardNums);
    },

    //亮听后翻开玩家手牌
    setLiangedShowHandCard : function(seat, handCardNums){
        this.mPlayers[seat].setLiangedShowHandCard(handCardNums);
    },

    //返回滑动出牌的牌
    getSelectMoveSprite : function(){
        if (this.mSelectMoveSprite == null){
            this.mSelectMoveSprite = this.getKwxCard();

        }
        this.mSelectMoveSprite.setVisible(false);

        return this.mSelectMoveSprite;
    },

    //停止胡牌动画
    setStopWinAnim : function(){
        this.mWinDian1.stopAllActions();
        this.mWinDian1.setVisible(false);
        this.mWinDian2.setVisible(false);
    },

    //胡牌动画
    setWinAnim : function(roundEndInfo){
        this.scheduleOnce(function (dt) {

            //是否有赢家
            var isWin = false;
            //是否是自摸
            var isZimo = false;
            //自摸玩家座位号
            var zimoSeat = -1;
            //不是自摸的话 点炮的座位号
            var dianpaiSeat = -1;

            //翻牌
            for (var seat = 0; seat < 3; seat++){
                var logicSeat = mo.kwxTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
                var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];
                if (seatUser == null){
                    continue;
                }

                var zimo = false;
                var winCard = seatUser.WinCard;

                //记录胡牌相关
                var isHU = seatUser.WinCard != null && seatUser.WinCard != 0;
                if (isHU){
                    isWin = true;
                    zimo = seatUser.WinCardSource == 0;
                    isZimo = zimo;
                    if (isZimo){
                        zimoSeat = seat;
                    }else{
                        dianpaiSeat = mo.kwxTableStatus.getViewForLogic(seatUser.WinCardSource - 1, roundEndInfo.MySeat - 1);
                    }
                }

                //翻开手牌
                if (seat != 0){
                    var handCardNums = seatUser.HandCards;
                    handCardNums.sort();
                    if (zimo){
                        for (var i = 0; i < handCardNums.length; i++){
                            if (handCardNums[i] == winCard){
                                handCardNums.splice(i, 1);
                                break;
                            }
                        }
                        handCardNums.push(winCard);
                    }

                    if (handCardNums.length == this.mPlayers[seat].inhandCards.length){
                        for (var i = 0; i < this.mPlayers[seat].inhandCards.length; i++){
                            this.mPlayers[seat].inhandCards[i].setCardNum(handCardNums[i]);
                            this.mPlayers[seat].inhandCards[i].setCardKind(seat, 1);

                            var isFromCard = zimo && i == this.mPlayers[seat].inhandCards.length - 1;
                            var pos = mo.kwxCardHelper.getInhandCardPos(seat, i, seatUser.Groups.length, isFromCard);
                            this.mPlayers[seat].inhandCards[i].setPosition(pos);
                        }
                    }
                }
            }

            if (isWin){
                var winCardSprite = null;
                var scale = 1;
                var rotate = 0;
                var fireWidth = 56;
                if (isZimo){
                    winCardSprite = this.mPlayers[zimoSeat].inhandCards[this.mPlayers[zimoSeat].inhandCards.length - 1];
                    if (zimoSeat == 0){
                        scale = mo.kwxCardHelper.inhandCardSize.height * mo.kwxCardHelper.inhandCardScale / fireWidth;
                        rotate = 90;
                    }else{
                        scale = mo.kwxCardHelper.leftOpenCardSize.width * mo.kwxCardHelper.leftInhandShowCardScale / fireWidth;
                    }
                }else{
                    winCardSprite = this.mPlayers[dianpaiSeat].dealCards[this.mPlayers[dianpaiSeat].dealCards.length - 1];
                    if (dianpaiSeat == 0){
                        scale = mo.kwxCardHelper.inhandCardSize.height * mo.kwxCardHelper.dealScale / fireWidth;
                        rotate = 90;
                    }else{
                        scale = mo.kwxCardHelper.leftOpenCardSize.width * mo.kwxCardHelper.leftDealScale / fireWidth;
                    }
                }
                if (winCardSprite == null){
                    return;
                }
                //2人场特效
                if (mo.gameMsgManager.kwxGameModel == 2){
                    rotate = 90;
                }

                var rect = winCardSprite.getBoundingBox();

                var spriteFrames = [];
                for (var i = 0; i <= 60; i++){
                    var frame = cc.spriteFrameCache.getSpriteFrame("cardFire" + i + ".png.png");
                    if (frame == null){
                        break;
                    }
                    spriteFrames.push(frame);
                }
                var animation = new cc.Animation(spriteFrames);
                animation.setDelayPerUnit(0.05);
                this.mWinDian1.setVisible(false);
                this.mWinDian1.setAnchorPoint(cc.p(0.5, 0.5));
                this.mWinDian1.setPosition(cc.p(cc.rectGetMidX(rect), cc.rectGetMidY(rect)));
                this.mWinDian1.setScale(scale);
                this.mWinDian1.setRotation(rotate);
                this.mWinDian1.runAction(cc.sequence(
                    cc.delayTime(1),
                    cc.show()
                ));
                this.mWinDian1.runAction(cc.repeatForever(cc.animate(animation)));

                // var spriteFrames2 = [];
                // for (var i = 21; i >= 0; i--){
                //     var frame = cc.spriteFrameCache.getSpriteFrame("cardLightning" + i + ".png");
                //     if (frame == null){
                //         break;
                //     }
                //     spriteFrames2.push(frame);
                // }
                // var animation2 = new cc.Animation(spriteFrames2);
                // animation2.setDelayPerUnit(0.05);
                // this.mWinDian2.setVisible(true);
                // this.mWinDian2.setAnchorPoint(cc.p(0.5, 0.5));
                // this.mWinDian2.setPosition(cc.p(cc.rectGetMidX(rect), cc.rectGetMidY(rect) + 100));
                // this.mWinDian2.runAction(cc.sequence(cc.animate(animation2), cc.hide()));
            }
        }, 0.5);
    },

    //短线重连
    setReconnectStatus : function(tableStatus){

        // for (var seat = 0; seat < 3; seat++){
        //     //开口牌
        //     for (var i = 0; i < 3; i++){
        //         var cards = [];
        //         var openCards = [21, 22, 23, 24];
        //         for (var j = 0; j < openCards.length; j++){
        //             var card = this.getKwxCard();
        //             card.setCardNum(openCards[j]);
        //             cards.push(card);
        //         }
        //         var openCard = mo.kwxCardHelper.getOpenCardStruct(cards, 4);
        //         this.mPlayers[seat].openCards.push(openCard);
        //     }
        //
        //     //手牌
        //     var handCards = [];
        //     var handCardNums = mo.kwxCardHelper.getRandomCardNum(14);
        //     if (false){
        //         //其他玩家手牌不显示
        //         for (var i = 0; i < handCardNums.length; i++){
        //             var card = this.getKwxCard();
        //             card.setCardNum(0);
        //             handCards.push(card);
        //         }
        //     }else{
        //         for(var i = 0; i < handCardNums.length; i++){
        //             var num = handCardNums[i];
        //             var card = this.getKwxCard();
        //             card.setCardNum(num);
        //             handCards.push(card);
        //         }
        //     }
        //     this.mPlayers[seat].inhandCards = handCards;
        //
        //     //设置打出去的牌
        //     var dealCards = [];
        //     var dealCardNums = mo.kwxCardHelper.getRandomCardNum(20);
        //     for(var i = 0; i < dealCardNums.length; i++){
        //         var num = dealCardNums[i];
        //         var card = this.getKwxCard();
        //         card.setCardNum(num);
        //         dealCards.push(card);
        //     }
        //     this.mPlayers[seat].dealCards = dealCards;
        //
        //
        //     this.mPlayers[seat].resetPlayer();
        // }
        //
        // //自己的操作
        // var myUseActionRequest = [
        //     {code:0, Cg:"test"},
        //     {code:2},
        //     {code:3},
        //     {code:4},
        //     {code:8}
        // ];
        // mo.kwxTable.mKwxTableUILayer.mKwxTableOperatingLayer.showOperating(true, myUseActionRequest);
        //
        // //显示玩家听牌列表UI
        // this.scheduleOnce(function(dt){
        //     for (var i = 0; i < 3; i++) {
        //         var tingCardNums = [22, 21];
        //         tingCardNums.sort();
        //         mo.kwxTable.mKwxTableUILayer.setShowTing(i, tingCardNums);
        //     }
        // }, 1.0);
        //
        // return;
        //TODO
        if (tableStatus.Step == 2){
            //打牌中才处理

            //找到最后打出的牌
            if (tableStatus.LastAction){
                var lastAction = tableStatus.LastAction;
                if (lastAction.code == 7 || lastAction.code == 0){
                    this.mLastDealCardSeat = mo.kwxTableStatus.getViewForLogic(tableStatus.LastActionSeat - 1, tableStatus.MySeat - 1);
                }
            }

            //放上玩家的牌
            var seatUsers = [tableStatus.SeatUser1, tableStatus.SeatUser2, tableStatus.SeatUser3];
            for (var seat = 0; seat < 3; seat++){
                var logicSeat = mo.kwxTableStatus.getLogicForView(seat, tableStatus.MySeat - 1);
                var seatUser = seatUsers[logicSeat];
                if (seatUser == null){
                    continue;
                }

                //设置开口牌
                for (var i = 0; i < seatUser.Groups.length; i++){
                    var cardGroup = seatUser.Groups[i];
                    var cards = [];
                    for (var j = 0; j < cardGroup.Cards.length; j++){
                        var card = this.getKwxCard();
                        card.setCardNum(cardGroup.Cards[j]);
                        cards.push(card);
                    }
                    var openCard = mo.kwxCardHelper.getOpenCardStruct(cards, cardGroup.GroupType);
                    this.mPlayers[seat].openCards.push(openCard);
                }

                //设置手牌
                var handCards = [];
                if (seatUser.HandCards.length == 0){
                    //其他玩家手牌不显示
                    for (var i = 0; i < seatUser.LeftCards; i++){
                        var card = this.getKwxCard();
                        card.setCardNum(0);
                        handCards.push(card);
                    }
                }else{
                    for(var i = 0; i < seatUser.HandCards.length; i++){
                        var num = seatUser.HandCards[i];
                        var card = this.getKwxCard();
                        card.setCardNum(num);
                        handCards.push(card);
                    }
                }
                this.mPlayers[seat].inhandCards = handCards;

                //设置打出去的牌
                var dealCards = [];
                for(var i = 0; i < seatUser.PlayCards.length; i++){
                    var num = seatUser.PlayCards[i];
                    var card = this.getKwxCard();
                    card.setCardNum(num);
                    dealCards.push(card);
                }
                this.mPlayers[seat].dealCards = dealCards;

                this.mPlayers[seat].resetPlayer();
            }
        }
    }
});